package com.youlu.campus.admin.community.controller;

import com.youlu.campus.admin.annotation.AuthParamTypeEnum;
import com.youlu.campus.admin.annotation.RequestActivityAuth;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.ActivityPayButtonConfigVO;
import com.youlu.campus.entity.VO.req.ActivityPayConfigVO;
import com.youlu.campus.entity.VO.req.ActivityPayCopy;
import com.youlu.campus.entity.VO.req.QueryPayConfigVO;
import com.youlu.campus.entity.course.UserAllTopRecordService;
import com.youlu.campus.entity.course.dto.UserOnTopRequest;
import com.youlu.campus.service.activity.impl.ActivityPayButtonConfigService;
import com.youlu.campus.service.activity.impl.ActivityPayConfigService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class ActivityPayConfigController {

    @Autowired
    private ActivityPayConfigService activityPayConfigService;
    @Autowired
    private ActivityPayButtonConfigService activityPayButtonConfigService;
    @Autowired
    private UserAllTopRecordService userAllTopRecordService;

    @PostMapping("/activity/pay-config/save")
    @RequestActivityAuth(activityId = "#payButtonConfig.activityId",message = "当前用户无权限修改")
    public RestResult save(@RequestBody @Validated ActivityPayConfigVO payButtonConfig, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestResultBuilder.builder().failure(bindingResult.getFieldError().getDefaultMessage()).build();
        }
        activityPayConfigService.save(payButtonConfig);
        return RestResultBuilder.builder().success().build();
    }

    @GetMapping("/activity/pay-config/detail")
    public RestResult detail(@RequestParam("id") String id) {
        return RestResultBuilder.builder().success().data(activityPayConfigService.detail(id)).build();
    }

    @DeleteMapping("/activity/pay-config/delete")
    public RestResult delete(@RequestParam("id") String id) {
        activityPayConfigService.delete(id);
        return RestResultBuilder.builder().success().build();
    }

    @PostMapping("/activity/pay-config/list")
    public RestResult list(@RequestBody QueryPayConfigVO queryPayConfigVO) {
        queryPayConfigVO.setUserId(AuthUtils.getUserId());
        return RestResultBuilder.builder().success().data(activityPayConfigService.list(queryPayConfigVO)).build();
    }

    /**
     * 活动收款置顶
     * @param request
     * @return
     */
    @PostMapping("/activity/pay-config/on/top")
    public QResult activityList(@Validated @RequestBody UserOnTopRequest request) {
        request.setUserId(AuthUtils.getUserId());
        try {
            return QResult.success(userAllTopRecordService.recordOnTop(request));
        } catch (Exception e) {
            log.error(":>>> 活动置顶:{}", e);
            return QResult.fail("活动置顶错误");
        }
    }


    @GetMapping("/activity/pay-config/button/detail")
    public RestResult buttonDetail(@RequestParam("id") String id) {
        return RestResultBuilder.builder().success().data(activityPayButtonConfigService.detail(id)).build();
    }

    @GetMapping("/activity/pay-config/button/list")
    public RestResult buttonList(@RequestParam("payConfigPageId") String payConfigPageId) {
        return RestResultBuilder.builder().success().data(activityPayButtonConfigService.findByPayConfigId(payConfigPageId)).build();
    }

    @PostMapping("/activity/pay-config/button/save")
    @RequestActivityAuth(activityId = "#activityPayButtonConfig.payConfigPageId",message = "当前用户无权限修改",paramType = AuthParamTypeEnum.PAY_CONFIG)
    public RestResult buttonSave(@RequestBody @Validated ActivityPayButtonConfigVO activityPayButtonConfig,
                                 BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestResultBuilder.builder().failure(bindingResult.getFieldError().getDefaultMessage()).build();
        }
        activityPayButtonConfigService.save(activityPayButtonConfig);
        return RestResultBuilder.builder().success().build();
    }

    @DeleteMapping("/activity/pay-config/button/delete")
    @RequestActivityAuth(activityId = "#id",message = "当前用户无权限修改",paramType = AuthParamTypeEnum.PAY_BUTTON)
    public RestResult buttonDelete(@RequestParam("id") String id) {
        activityPayButtonConfigService.delete(id);
        return RestResultBuilder.builder().success().build();
    }

    @PostMapping("/activity/pay-config/copy")
    public RestResult payCopy(@RequestBody @Validated ActivityPayCopy activityPayCopy,
                              BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestResultBuilder.builder().failure(bindingResult.getFieldError().getDefaultMessage()).build();
        }
        activityPayConfigService.payCopy(activityPayCopy);
        return RestResultBuilder.builder().success().build();
    }

}
